<?php
class Admin_ConfigController extends YouNet_Controller_Action
{   
    public function init() 
    {
        parent::init();
        $this->_helper->layout->setLayout('layout_facebook');
    }
    
    public function preDispatch()
    {
        parent::preDispatch();
        
        // View
        $this->view->pages = $this->_social->getUserPages();        
        $this->view->id_page = $this->_getParam("id_page");
        $this->view->params = $this->_params;
    }
    
    public function fansAction()
    {
        $this->_image = new Admin_Form_Image();
        $this->_html = new Admin_Form_Html();
        $this->_iframe = new Admin_Form_Iframe();
        
        $page_type  = 0;// fans
        
        $this->do_process($page_type,$this->_image,$this->_html,$this->_iframe);
        
        $this->view->image_form = $this->_image;
        $this->view->html_form = $this->_html;
        $this->view->iframe_form = $this->_iframe;
        
        $this->view->menu_active = 1;//fans
    }
    
    public function nonfansAction()
    {
        $this->_image = new Admin_Form_Image();
        $this->_html = new Admin_Form_Html();
        $this->_iframe = new Admin_Form_Iframe();
        $this->_off = new Admin_Form_Off();
        
        $page_type  = 1;//non fans
               
        $this->do_process($page_type,$this->_image,$this->_html,$this->_iframe);
        
        $this->view->image_form = $this->_image;
        $this->view->html_form = $this->_html;
        $this->view->iframe_form = $this->_iframe;
        $this->view->off_form = $this->_off;
        $this->view->menu_active = 2;//fans
    }
    
    public function do_process($page_type,$form_image,$form_html,$form_iframe)
    {
        $type = 1;//default image is selected
        
        if ($page_type == 1){
            $type = 0 ;//default image is selected
        }
        $id_page = $this->_getParam('id_page'); 
        
        /********get data from db*******/
        $tbl_config = Core_Model_DbTable_Configs::getInstance();    
        $config_content = $tbl_config->getconfigs($id_page,$page_type);
        /********get data from db END*******/ 

        if ($config_content)// load data from db
        { 
            if ($config_content["image"] != "")
            {
                 $form_image->image->setRequired(false);
                 $form_image->addThumbnail($config_content["image"]);
            }

            $type = $config_content["content_type"];
            //$check_show = $config_content["check_show"];
            $form_image->populate($config_content->toArray());
            $form_html->populate($config_content->toArray());
            $form_iframe->populate($config_content->toArray());
        }
        
        if( isset($_POST['submit']) )
        {
            $formdata = $this->getRequest()->getPost();
           
            $type = $formdata['hdf_type'];

            if ($type == 0 && $page_type = 1)//non fans and checked off
            {
                $tbl_config = Core_Model_DbTable_Configs::getInstance(); 
                $status= $tbl_config ->update_checkshow($type,$page_type,$id_page);
                $this->view-> is_updated = $status;
            }  
            else if ($type == 1)//process sample for fans and non fans
            {
                $status = $this->check_form_valid($form_image,$formdata,$id_page,$page_type);
            } 
            else if ($type == 2)//process sample for fans and non fans
            {
                 $status = $this->check_form_valid($form_html,$formdata,$id_page,$page_type); 
            } 
            else if ($type == 3)//process sample for fans and non fans
            {
                 $status =  $this->check_form_valid($form_iframe,$formdata,$id_page,$page_type);
            }
        }
        
        $this->view->content_type = $type;
    }
    
    /**
     * Description : process validate data ,if valid then insert data
     * @param type $form
     * @param type $formData
     */
    public function check_form_valid($form, $formData,$id_page,$page_type)
    {
        if ($form->isValid($formData)) 
        {
            if ($form->image)
            {
                $form->getImageNameAndReceive();
            }
            
            $arrValidValues = $form->getValues();
            $content_type = $arrValidValues['hdf_type'];
            
            //insert data
            $tbl_config = Core_Model_DbTable_Configs::getInstance(); 
            $status=  $tbl_config->insert_config($arrValidValues,$id_page,$page_type);
            
            if ($content_type == 1)
            {
                if ($arrValidValues["image"])
                {
                    $form -> addThumbnail($arrValidValues["image"]);
                }
            }
            $this->view-> is_updated = $status;
        }
    }
}